package com.example.datachange.ai;

import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.concurrent.*;

public class Home {

    private static final ThreadPoolExecutor EXECUTOR = new ThreadPoolExecutor(
            20,
            200,
            10,
            TimeUnit.SECONDS,
            new LinkedBlockingDeque<>(100000),
            Executors.defaultThreadFactory(),
            new ThreadPoolExecutor.AbortPolicy());


    private static  final int TOTAL = 66;
    private static  final int SLEEP = 300;

    private static  final String PREFIX = "[";
    private static  final String SUFFIX = "]";

    private static  final String TITLE_DEF = "资源检索中";

    private static final Map<String, String> RESOURCE = new HashMap<>();
    static {
        RESOURCE.put("1", "你的故乡是疆员辽阔的省份——新疆！");
        RESOURCE.put("2", "你的故乡是沿海城市，水产品盛行的故乡——福建！");
        RESOURCE.put("3", "你的故乡是华夏文明发源地——河南！");
    }

    public static void main(String[] args) throws InterruptedException, ExecutionException {
        System.out.println("请选择你的故乡：1. 新疆 2. 福建 3. 河南");
        String home = new Scanner(System.in).next();
        System.out.println("等待输出：");

        CompletableFuture<Void> task = CompletableFuture.runAsync( Home::progressBar, EXECUTOR);
        CompletableFuture<Void> task1 = CompletableFuture.runAsync(() -> { throw new NullPointerException(); }, EXECUTOR);

        //等到所有任务都完成
        CompletableFuture.allOf(task, task1).get();

        Thread.sleep(1000);
        System.out.print('\r');

        String s = RESOURCE.get(home);
        if(StringUtils.isEmpty(s)){
            System.out.println("你特么搞什么啊！");
            Thread.sleep(3000);
            main(args);
            return;
        }

        EXECUTOR.shutdown();
        System.out.println(s);
    }

    private static void init(){
        StringBuilder s = new StringBuilder().append(PREFIX);
        for (int i = 0; i < Home.TOTAL; i++) {
            s.append(" ");
        }
        s.append(SUFFIX);
        System.out.print(s.toString());
    }

    private static void executeing(){
        System.out.println();
        String title = TITLE_DEF;
        System.out.print(TITLE_DEF);
        for (int i = 0; i < TOTAL; i++) {
            try {
                Thread.sleep(SLEEP);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.print('\r');
            if (i == TOTAL - 1) {
                break;
            }
            title += ".";
            if (title.length() > 11) {
                System.out.print(TITLE_DEF);
                title = TITLE_DEF;
                continue;
            }
            System.out.print(title);
        }
    }

    private static void progressBar(){
        System.out.println();
        init();
        for (int i = 0; i < TOTAL; i++) {
            try {
                if(i <= TOTAL * 0.5){
                    Thread.sleep((long) (SLEEP * 0.5));
                }else if(i <= TOTAL * 0.75){
                    Thread.sleep((long) (SLEEP * 0.75));
                }else if(i >= TOTAL * 0.95){
                    Thread.sleep(SLEEP * 4);
                }else if(i >= TOTAL * 0.9){
                    Thread.sleep(SLEEP * 2);
                }else{
                    Thread.sleep(SLEEP);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.print('\r');
            StringBuilder s = new StringBuilder().append(PREFIX);
            for (int j = 0; j < TOTAL; j++) {
                if(j <= i){
                    s.append("■");
                }else{
                    s.append(" ");
                }
            }
            s.append(SUFFIX);
            System.out.print(s.toString());
        }
    }
}
